home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / TMPSTORE.PY < prev    next >
Encoding:
Python Source  |  2000-08-08  |  6.5 KB  |  177 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64.  
  65. import POSException
  66. from utils import p64, u64
  67.  
  68. class TmpStore:
  69.     _transaction=_isCommitting=None
  70.  
  71.     def __init__(self, base_version, file=None):
  72.         if file is None:
  73.             import tempfile
  74.             file=tempfile.TemporaryFile()
  75.  
  76.         self._file=file
  77.         self._index={}
  78.         self._pos=self._tpos=0
  79.         self._bver=base_version
  80.         self._tindex=[]
  81.         self._db=None
  82.  
  83.     def __del__(self): self.close()
  84.  
  85.     def close(self):
  86.         self._file.close()
  87.         del self._file
  88.         del self._index
  89.         del self._db
  90.  
  91.     def getName(self): return self._db.getName()
  92.     def getSize(self): return self._pos
  93.  
  94.     def load(self, oid, version):
  95.         #if version is not self: raise KeyError, oid
  96.         pos=self._index.get(oid, None)
  97.         if pos is None:
  98.             return self._storage.load(oid, self._bver)
  99.         file=self._file
  100.         file.seek(pos)
  101.         h=file.read(24)
  102.         if h[:8] != oid:
  103.             raise POSException.StorageSystemError, 'Bad temporary storage'
  104.         return file.read(u64(h[16:])), h[8:16]
  105.         
  106.     def modifiedInVersion(self, oid):
  107.         if self._index.has_key(oid): return 1
  108.         return self._db._storage.modifiedInVersion(oid)
  109.  
  110.     def new_oid(self): return self._db._storage.new_oid()
  111.  
  112.     def registerDB(self, db, limit):
  113.         self._db=db
  114.         self._storage=db._storage
  115.  
  116.     def store(self, oid, serial, data, version, transaction):
  117.         if transaction is not self._transaction:
  118.             raise POSException.StorageTransactionError(self, transaction)
  119.         file=self._file
  120.         pos=self._pos
  121.         file.seek(pos)
  122.         l=len(data)
  123.         file.write(oid+serial+p64(l))
  124.         file.write(data)
  125.         self._tindex.append((oid,pos))
  126.         self._pos=pos+l+24
  127.         return serial
  128.         
  129.     def tpc_abort(self, transaction):
  130.         if transaction is not self._transaction: return
  131.         del self._tindex[:]
  132.         self._transaction=None
  133.         self._pos=self._tpos
  134.  
  135.     def tpc_begin(self, transaction):
  136.         if self._transaction is transaction: return
  137.         self._transaction=transaction
  138.         del self._tindex[:]   # Just to be sure!
  139.         self._pos=self._tpos
  140.  
  141.     def tpc_vote(self, transaction): pass
  142.  
  143.     def tpc_finish(self, transaction, f=None):
  144.         if transaction is not self._transaction: return
  145.         if f is not None: f()
  146.         index=self._index
  147.         tindex=self._tindex
  148.         for oid, pos in tindex: index[oid]=pos
  149.         del tindex[:]
  150.         self._tpos=self._pos
  151.  
  152.     def undoLog(self, first, last, filter=None): return ()
  153.     
  154.     def versionEmpty(self, version):
  155.         if version is self: return len(self._index)
  156.